package com.vitria.m3oui.test.px;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
	
	private static Log log_ = LogFactory.getLog(ZipUtil.class);
	
	private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,  
            File srcFile, String prefix) throws IOException, FileNotFoundException {  
        ZipEntry zipEntry;  
        String entryName = srcFile.getAbsolutePath();
        entryName = entryName.replace(prefix, "");
        if (entryName.startsWith(File.separator))
        		entryName = entryName.substring(1);
        if (srcFile.isDirectory()) {
            if (!"".equals(entryName)) {
            	log_.info("creating directory - " + srcFile.getAbsolutePath()  
                        + "  entryName=" + entryName);  
  
                zipEntry = new ZipEntry(entryName + File.separator);
                zos.putNextEntry(zipEntry);  
            }
  
            File srcFiles[] = srcFile.listFiles();
            for (int i = 0; i < srcFiles.length; i++) {
                writeRecursive(zos, bo, srcFiles[i], prefix);  
            }
        } else {
        	log_.info("writing file- " + srcFile.getAbsolutePath() + "  entryName="  
                    + entryName);  
            BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));  
            zipEntry = new ZipEntry(entryName);
            zos.putNextEntry(zipEntry);
            byte[] buffer = new byte[2048];  
            int readCount = bi.read(buffer);
  
            while (readCount != -1) {
                bo.write(buffer, 0, readCount);
                readCount = bi.read(buffer);  
            }
            bo.flush();
            bi.close();  
        }
        srcFile.delete();
    } 
	
	public static boolean writeToZip(File srcDir, String zipFileName){
		FileOutputStream f = null;
		boolean result = true;
		try {
			f = new FileOutputStream(zipFileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
		ZipOutputStream zos = new ZipOutputStream(csum);  
        zos.setEncoding("utf-8");
        BufferedOutputStream out = new BufferedOutputStream(zos);
        zos.setMethod(ZipOutputStream.DEFLATED);
        try {
			ZipUtil.writeRecursive(zos, out, srcDir, srcDir.getAbsolutePath());
			log_.info("Export to zip file successfully!");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			result = false;
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
        try {
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
			result = false;
		}
		return result;
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) {
	}

}
